global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A25_mp_outliers.log, replace name(tabl)

capture noi {  

    texdoc init ${tab_dir}/appendix/Table_A25_mp_outliers.tex, replace
    texdoc stlog, nolog

    use ${final_dir}/MP_DHOZ_ipc_comparison.dta, clear
    keep if total >= 100 & total_MP_patent >= 100
    gen diff_sh = share_anyclassification - share_auto_puettmann

    * list positive outliers (among auto95 codes) 
    preserve
    keep if auto95 == 1
    gsort -diff_sh
    forval i = 1/6 {
        texdoc local p`i'_code = ipc6xx[`i']
        local c = ipc6xx[`i']
        texdoc local p`i'_dhoz_s : display %9.2f share_auto95[`i']
        sleep 500
        texdoc local p`i'_dhoz : display %9.2f share_anyclassification[`i']
        sleep 500
        texdoc local p`i'_mp : display %9.2f share_auto_puettmann[`i']
        sleep 500
        di "positive outlier code `i' : `p`i'_code'"
    }
    restore

    *labelling

    texdoc local B25J17_descr "Manipulators (joints)"
    texdoc local D01H9_descr "Textile machines (arrangements for replacing or removing various elements)"
    texdoc local B25J15_descr "Manipulators (gripping heads)"
    texdoc local B23P23_descr "Metal working machines (specified combinations n.e.c)"
    texdoc local B25J19_descr "Manipulators (accessories)"
    texdoc local B33Y70_descr "3D printing materials"

    * list negative outliers (among auto90 machinery codes)
    preserve
    keep if auto90 == 0
    keep if machinery_field == 1
    sort diff_sh
    forval i = 1/6 {
        texdoc local n`i'_code = ipc6xx[`i']
        sleep 500
        texdoc local n`i'_dhoz_s : display %9.2f share_auto95[`i']
        sleep 500
        texdoc local n`i'_dhoz : display %9.2f share_anyclassification[`i']
        sleep 500
        texdoc local n`i'_mp : display %9.2f share_auto_puettmann[`i']
        di "negative outlier code `i' : `n`i'_code'"
    }
    restore

    *labelling
    texdoc local B66B2201_descr "Control systems of elevators"
    texdoc local B66B3_descr "Elevators (signalling and indicating device applications)"
    texdoc local B41J23_descr "Typerwriters / printing machines (power drive)"
    texdoc local B66B1_descr "Elevators (control systems)"
    texdoc local B41J19_descr "Typerwriters / printing machines (characters and line spacing mechanisms)"
    texdoc local B41J5_descr "Typerwriters / printing machines (controlling character selection)"

    * output table (via texdoc)
    texdoc local notes "This table lists the 6 auto95 codes with the largest positive difference between the prevalence of automation keywords based on our classification and the share of automation patents according to MP in their data; and the 6 non-auto90 codes with the largest negative difference between the two measures. We additionaly list the share of patents classified auto95 according to our definition. We restrict attention to codes with at least 100 patents in both datasets."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{\hspace*{0.3em}}llccc@{}}
    \toprule
    \hspace*{-0.3em}Code & Simplified description & \makecell[tc]{DHOZ \\ Keyword prevalence} & \makecell[tc]{DHOZ \\ Share auto95}  & \makecell[tc]{MP \\ Share auto95} \\
    \midrule
    \noalign{\smallskip}
    \multicolumn{3}{@{}l}{Positive outliers among auto95 codes\hspace*{-0.3em}} \\
    \noalign{\smallskip}
    `p1_code' &  `B25J17_descr' & `p1_dhoz' & `p1_dhoz_s' & `p1_mp' \\
    `p2_code' &  `D01H9_descr'  & `p2_dhoz' & `p2_dhoz_s' & `p2_mp' \\
    `p3_code' &  `B25J15_descr' & `p3_dhoz' & `p3_dhoz_s' & `p3_mp' \\
    `p4_code' &  `B23P23_descr' & `p4_dhoz' & `p4_dhoz_s' & `p4_mp' \\
    `p5_code' &  `B25J19_descr' & `p5_dhoz' & `p5_dhoz_s' & `p5_mp' \\
    `p6_code' &  `B33Y70_descr' & `p6_dhoz' & `p6_dhoz_s' & `p6_mp' \\  
            
    \noalign{\smallskip}
    \multicolumn{3}{@{}l}{Negative outliers among non-auto90 codes\hspace*{-0.3em}} \\
    \noalign{\smallskip}

    `n1_code' &  `B66B2201_descr' & `n1_dhoz' & `n1_dhoz_s' & `n1_mp' \\
    `n2_code' &  `B66B3_descr'  & `n2_dhoz' & `n2_dhoz_s' & `n2_mp' \\
    `n3_code' &  `B41J23_descr' & `n3_dhoz' & `n3_dhoz_s' & `n3_mp' \\
    `n4_code' &  `B66B1_descr' & `n4_dhoz' & `n4_dhoz_s' & `n4_mp' \\
    `n5_code' &  `B41J19_descr' & `n5_dhoz' & `n5_dhoz_s' & `n5_mp' \\
    `n6_code' &  `B41J5_descr' & `n6_dhoz' & `n6_dhoz_s' & `n6_mp' \\  

    \bottomrule
    \end{tabular}

    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}


